﻿#include "conclusionlist.h"
#include "ui_conclusionlist.h"
#include "rtfform.h"

conclusionList::conclusionList(QWidget *parent) :
    QFrame(parent),
    ui(new Ui::conclusionList)
{
    ui->setupUi(this);

    fillStuff();
    dbModel = new myDataModel(this,ui->table,getQuery());

    dbModel->setHeader(QStringList()<<"№"<<"Дата"<<"Организация"<<"Должность"<<"Имя"<<"Оборудование"<<"S/N"<<"Инв. номер"<<"Год"<<"Заключение"<<"Сотрудник");

    ui->table->setColumnHidden( CDB_printform, true);
    ui->table->show();
    ui->table->scrollTo(dbModel->index(dbModel->rowCount()-1,0));

    connect(ui->table->selectionModel(), SIGNAL(currentRowChanged ( const QModelIndex &, const QModelIndex &)),
            this, SLOT(currentRowChanged( const QModelIndex &, const QModelIndex &)));

    ui->table->horizontalHeader()->setResizeMode(CDB_enumber,QHeaderView::ResizeToContents);
    ui->table->horizontalHeader()->setResizeMode(CDB_date,QHeaderView::ResizeToContents);
    ui->table->horizontalHeader()->setResizeMode(CDB_name,QHeaderView::ResizeToContents);
    ui->table->horizontalHeader()->setResizeMode(CDB_who,QHeaderView::ResizeToContents);
    ui->table->horizontalHeader()->setResizeMode(CDB_year,QHeaderView::ResizeToContents);
    ui->table->horizontalHeader()->setResizeMode(CDB_serial,QHeaderView::Stretch);
    ui->table->horizontalHeader()->setResizeMode(CDB_inventory,QHeaderView::Stretch);
    ui->table->horizontalHeader()->setResizeMode(CDB_conclusion,QHeaderView::Stretch);
    ui->table->horizontalHeader()->setResizeMode(CDB_org,QHeaderView::Stretch);
    ui->table->horizontalHeader()->setResizeMode(CDB_post,QHeaderView::Stretch);
    ui->table->horizontalHeader()->setResizeMode(CDB_equipment,QHeaderView::Stretch);

    ui->printBox->addItems(QStringList()<<"novsu"<<"maximov"<<"novintex"<<"novintexclass");

}

conclusionList::~conclusionList()
{
    delete ui;
}

void conclusionList::updateList()
{
    dbModel->setQuery(getQuery());
}

void conclusionList::on_delSearchButton_clicked()
{
    ui->searchEdit->clear();
    dbModel->setFilterFixedString("");
}

void conclusionList::on_searchButton_clicked()
{
    dbModel->setFilterFixedString(ui->searchEdit->text());
}

void conclusionList::on_searchEdit_returnPressed()
{
    dbModel->setFilterFixedString(ui->searchEdit->text());
}

void conclusionList::fillStuff()
{
    QSqlQuery query("SELECT * FROM stafflist ORDER by who DESC");
    while(query.next())ui->whoSelect->insertItem(0,query.value(0).toString());
}

void conclusionList::currentRowChanged(const QModelIndex &current, const QModelIndex &previous)
{
    ui->orgEdit->setText(dbModel->getData(CDB_org));
    ui->postEdit->setText(dbModel->getData(CDB_post));
    ui->nameEdit->setText(dbModel->getData(CDB_name));
    ui->equipmentEdit->setText(dbModel->getData(CDB_equipment));
    ui->serialEdit->setText(dbModel->getData(CDB_serial));
    ui->inventoryEdit->setText(dbModel->getData(CDB_inventory));
    ui->yearBox->setValue(dbModel->getData(CDB_year).toInt());
    ui->conclusionEdit->setPlainText(dbModel->getData(CDB_conclusion));
    ui->whoSelect->setEditText(dbModel->getData(CDB_who));
    ui->printBox->setCurrentIndex(dbModel->getData(CDB_printform).toInt());
}


QString conclusionList::getQuery()
{
        return "SELECT * FROM conclusionlist";
}

void conclusionList::on_addButton_clicked()
{
    if(ui->equipmentEdit->text().isEmpty()){
        QMessageBox::warning(0,"Ошибка","Введите название оборудования.");
        return;
    }
    QSqlQuery query;
    query.prepare("INSERT INTO conclusionlist "
                  "VALUES (DEFAULT,:date,:org,:post,:name,:equipment,:serial,:inventory,:year,:conclusion,:who,:printform)");
    query.bindValue(":date",QDate::currentDate());
    query.bindValue(":org",ui->orgEdit->text());
    query.bindValue(":post",ui->postEdit->text());
    query.bindValue(":name",ui->nameEdit->text());
    query.bindValue(":equipment",ui->equipmentEdit->text());
    query.bindValue(":serial",ui->serialEdit->text());
    query.bindValue(":inventory",ui->inventoryEdit->text());
    query.bindValue(":year",ui->yearBox->value());
    query.bindValue(":conclusion",ui->conclusionEdit->toPlainText());
    query.bindValue(":who",ui->whoSelect->currentText());
    query.bindValue(":printform",ui->printBox->currentIndex());
    query.exec();

    if(query.lastError().isValid())QMessageBox::warning(0,"Неверный запрос",query.lastError().text());

    dbModel->itemAdded();

}

void conclusionList::on_changeButton_clicked()
{
    if(ui->equipmentEdit->text().isEmpty()){
        QMessageBox::warning(0,"Ошибка","Введите название оборудования.");
        return;
    }

    QSqlQuery query;
    query.prepare("UPDATE conclusionlist "
                  "SET org=:org, post=:post, name=:name, equipment=:equipment, serial=:serial, inventory=:inventory, year=:year, conclusion=:conclusion, who=:who, printform=:printform "
                  "WHERE enumber=:number");

    query.bindValue(":number",dbModel->getData(CDB_enumber));
    query.bindValue(":org",ui->orgEdit->text());
    query.bindValue(":post",ui->postEdit->text());
    query.bindValue(":name",ui->nameEdit->text());
    query.bindValue(":equipment",ui->equipmentEdit->text());
    query.bindValue(":serial",ui->serialEdit->text());
    query.bindValue(":inventory",ui->inventoryEdit->text());
    query.bindValue(":year",ui->yearBox->value());
    query.bindValue(":conclusion",ui->conclusionEdit->toPlainText());
    query.bindValue(":who",ui->whoSelect->currentText());
    query.bindValue(":printform",ui->printBox->currentIndex());
    query.exec();

    if(query.lastError().isValid())QMessageBox::warning(0,"Неверный запрос",query.lastError().text());

    dbModel->itemChanged();

}

void conclusionList::on_printButton_clicked()
{
    if(ui->table->currentIndex().row()<0)return;

    QMap<QString,QString> formdata;
    formdata["<enumber>"]=dbModel->getData(CDB_enumber);
    formdata["<date>"]=dbModel->getData(CDB_date);
    formdata["<org>"]=dbModel->getData(CDB_org);
    formdata["<post>"]=dbModel->getData(CDB_post);
    formdata["<name>"]=dbModel->getData(CDB_name);
    formdata["<equipment>"]=dbModel->getData(CDB_equipment);
    formdata["<serial>"]=dbModel->getData(CDB_serial);
    formdata["<inventory>"]=dbModel->getData(CDB_inventory);
    formdata["<year>"]=dbModel->getData(CDB_year).toInt()==0?"":dbModel->getData(CDB_year);
    formdata["<conclusion>"]=dbModel->getData(CDB_conclusion);
    formdata["<who>"]=dbModel->getData(CDB_who);
    rtfForm rtf(QApplication::applicationDirPath()+"\\"+ui->printBox->currentText()+".rtf",formdata);

}
